package com.capgemini.teamtwo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.teamtwo.common.BusinessException;
import com.capgemini.teamtwo.common.ExceptionEnum;
import com.capgemini.teamtwo.entity.Dict;
import com.capgemini.teamtwo.mapper.DictMapper;
import com.capgemini.teamtwo.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict>
        implements DictService {
    @Autowired
    private DictMapper dictMapper;


    @Override
    public Integer addDict(Dict dict,String username) {

        //获取用户输入的标签值
        String lable = dict.getDictLable();
        //从数据库中查询这个标签
        Dict result = dictMapper.findByDictLable(lable);
        //若不为空则已存在 无需添加
        if (result != null) {
            throw new BusinessException(ExceptionEnum.LABLE_EXSITS_EXCEPTION);
        }
        dict.setCreateTime(new Date());
        if (dict.getDictLable() == null || dict.getDictValue() == null || dict.getDictLable() == null||dict.getDictName()==null) {

            throw  new BusinessException(ExceptionEnum.EMPTY_EXCEPTION) ;
        }

        dict.setUpdateBy(username);
        dict.setCreateBy(username);
        dict.setUpdateTime(new Date());
        dict.setCreateTime(new Date());
        int rows = dictMapper.insert(dict);

        if (rows != 1) {
            throw new BusinessException(ExceptionEnum.INSERT_EXCEPTION);
        }


        return rows;

    }

    @Override
    public Integer deldeteDict(Integer dictId) {
        //用传入的id在数据库中搜索
        Dict result = dictMapper.findByDictId(dictId);
        //如果搜索的值为空 即数据库中没有数据
        if (result==null){
            //则抛出异常
            throw new BusinessException(ExceptionEnum.DICT_NOT_EXSIT_EXCEPTIPN);
        }
        int rows = dictMapper.deleteById(dictId);
        if (rows!=1){
            //影响行数不为1 则删除失败 抛出异常
            throw  new BusinessException(ExceptionEnum.DELETE_EXCEPTION);
        }


        return rows;
    }

    @Override
    public Dict updateDictById(Integer dictId, Dict dict,String username) {
        //根据id在数据库中查询
        Dict result = dictMapper.findByDictId(dictId);
        if (result==null){//如果为空
            //则说明这个数据不存在 无法更新
            throw new BusinessException(ExceptionEnum.DICT_NOT_EXSIT_EXCEPTIPN);
        }
            //获得用户想更新的标签
        String lable = dict.getDictLable();
        Dict lable12 = dictMapper.findByDictLable(lable);
        if (lable12!=null){//不为空则标签存在
            //抛出异常
            throw  new BusinessException(ExceptionEnum.LABLE_EXSITS_EXCEPTION);
        }
        //执行更新操作
        dict.setUpdateBy(username);
        dict.setCreateBy(username);
        dict.setUpdateTime(new Date());
        dict.setCreateTime(new Date());
        int count= dictMapper.updateDictById(dict);
        if (count !=1){
            throw new BusinessException(ExceptionEnum.UPDATE_EXCEPTIOON);
        }

        return  dict;
    }

    @Override
    public List<Dict> selectDict(String dictLable, String dictValue, String dictType, String dictName) {
        List<Dict> list = dictMapper.selectDict(dictLable, dictValue, dictType,dictName);
        if (list==null){
            throw new BusinessException(ExceptionEnum.DICT_NOT_EXSIT_EXCEPTIPN);

        }


        return list;
    }

    @Override
    public List<Dict> selectByName(String dictName) {
        List<Dict> list = dictMapper.selectBydictName(dictName);
        if (list==null){
            throw  new BusinessException(ExceptionEnum.SELECT_EXCEPTION);
        }

        return list;
    }


}
